package com.fzshang.api.shiro;

import com.fzshang.commen.bean.Menu;
import com.fzshang.commen.bean.User;
import com.fzshang.commen.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 张子艺
 * @packge com.xbxkj.config.shiro
 * @data 2019-04-08 12:04
 * @project GoodLuck
 */
@Slf4j
public class ShiroRealm extends AuthorizingRealm {
    @Autowired
    UserService userService;

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //此方法中的值 为doGetAuthenticationInfo 存入的user
        User user = (User) principalCollection.getPrimaryPrincipal();
            if(user.getRoles()==null){
                return  simpleAuthorizationInfo;
            }
//        从shiro中取出登陆成功后的用户信息   并把角色名 给shiro控制
//        权限
        Set<String> roleSet = new HashSet<String>();
        roleSet.add("role:" + user.getRoles().getRoleName());
        simpleAuthorizationInfo.addRoles(roleSet);
        List<Menu> menuResourse = user.getRoles().getMenus();


        for (int i = 0; i < menuResourse.size(); i++) {
            if(menuResourse.get(i).getMenuType()==2){
                simpleAuthorizationInfo.addStringPermission("button:" + menuResourse.get(i).getMenuName());
            }
            simpleAuthorizationInfo.addStringPermission("menu:" + menuResourse.get(i).getMenuName());
        }

        return simpleAuthorizationInfo;
    }

    /**
     * 鉴权
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)  {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String requestUserName = token.getUsername();
        String requestPassWord = new String(token.getPassword());
        User user= userService.UserLogin(requestUserName,requestPassWord);

        //判断账户是否存在
        User username = userService.userNameIsNall(requestUserName);
        if(username==null){
            throw new IncorrectCredentialsException();
        }
        //判断账户对应的密码是否正确
        User passWord = userService.passWordIsNall(requestUserName,requestPassWord);
        if(passWord==null){
            throw new UnknownAccountException();
        }
        //这里只需要传入 数据库中查出的对象和 密码即可
        SimpleAuthenticationInfo authorizationInfo = new SimpleAuthenticationInfo(
                user,  //这里传对象
                user.getPassWord(),
                getName()
        );
        return authorizationInfo;
    }
}
